package fhv.itb.ec.book;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.MarshalledObject;
import java.rmi.RemoteException;
import java.rmi.activation.Activatable;
import java.rmi.activation.ActivationID;
import java.util.Vector;

public class BookImpl extends Activatable implements BookServer {

	private static final long serialVersionUID = 1L;

	Vector<BookEntry> m_entries;
	private String m_name;


	public BookImpl(ActivationID id, MarshalledObject data)
			throws RemoteException, IOException, ClassNotFoundException {
		super(id, 0);

		m_entries = new Vector<BookEntry>();

		// read in the book list
		// the url specified here (file) must be in the same folder, from which
		// the rmid daemon is started!!!
		File bookList = new File("BookList.csv");
		BufferedReader in = null;

		try {
			in = new BufferedReader(new FileReader(bookList));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		if (in != null) {

			String line = in.readLine();

			while (line != null) {
				BookEntry entry = new BookEntry();

				// split at semi-colons
				String[] info = line.split(";");

				if (info.length == 4) {
					entry.setTitle(info[0]);
					entry.setIsbn(info[1]);
					entry.setPublisher(info[2]);
					entry.setAuthors(info[3]);
					m_entries.add(entry);
				} else {
					System.out.println("info not complete: " + line);
				}

				line = in.readLine();
			}
			
			in.close();
		} // end of reading //

	}


	public void addEntry(BookEntry entry) {
		m_entries.add(entry);
	}


	public void deleteEntry(String isbn) {

		int i = 0;

		while (i < m_entries.size()) {

			if (m_entries.elementAt(i).getIsbn().equals(isbn)) {
				m_entries.remove(i);
				return;
			}

			i++;
		}

	}


	public BookEntry[] lookupBook(String searchText) {

		if (m_entries.isEmpty()) {
			return null;
		} else {
			Vector<BookEntry> temp = new Vector<BookEntry>();

			int i = 0;

			while (i < m_entries.size()) {
				if (m_entries.elementAt(i).getAuthors().contains(searchText)) {
					temp.add(m_entries.elementAt(i));
				} else if (m_entries.elementAt(i).getIsbn()
						.contains(searchText)) {
					temp.add(m_entries.elementAt(i));
				} else if (m_entries.elementAt(i).getTitle().contains(
						searchText)) {
					temp.add(m_entries.elementAt(i));
				} else if (m_entries.elementAt(i).getPublisher().contains(
						searchText)) {
					temp.add(m_entries.elementAt(i));
				}

				i++;
			}

			BookEntry[] be = new BookEntry[temp.size()];
			for (int j = 0; j < temp.size(); j++) {
				be[j] = temp.elementAt(j);
			}

			return be;
		}

	}


	public void save() {
		File bookList = new File("BookList.csv");
		BufferedWriter out = null;

		try {
			out = new BufferedWriter(new FileWriter(bookList));
			
			for (int i = 0; i < m_entries.size(); i++) {
				out.write(m_entries.elementAt(i).getTitle() + ";");
				out.write(m_entries.elementAt(i).getIsbn() + ";");
				out.write(m_entries.elementAt(i).getPublisher() + ";");
				out.write(m_entries.elementAt(i).getAuthors() + ";\n");
			}
			
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
